const SpiderBaseClass = require('./SpiderBaseClass');

const listSize = 120;//列表页面有多少个主播信息
const referer = 'https://www.panda.tv/all';

class SpiderPanda extends SpiderBaseClass {

  static firstListPage() {
    return 1;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {

    const res = await SpiderPanda.get(SpiderPanda.listPageUrl(page),
      {dataType: 'json', headers: SpiderPanda.headers(referer)});

    const list = res.data.data.items.map(room => {
      return {
        anchor: {
          room: room.id,
          name: room.userinfo.nickName,
          gender: 0, // 熊猫无法取得性别
          avatar: room.userinfo.avatar,
        },
        type: room.classification ? room.classification.cname : null,
        data: {
          num_watch: SpiderPanda.toNumber(room.person_num),
          num_live: 0,
          num_follower: 0,
          num_gift: 0,
        },
      };
    });
    return {
      list,
      page,
      getMoreInfo: true,
      nextPage: page + 1,
      hasNextPage: list.length >= listSize,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
    const [res, follower] = await Promise.all([
      SpiderPanda.get('https://www.panda.tv/' + room.anchor.room,
        {dataType: 'text',
          headers: SpiderPanda.headers(
            'https://www.panda.tv/' + room.anchor.room),
        }),
      SpiderPanda.get('https://www.panda.tv/room_followinfo?roomid=' +
        room.anchor.room,
        {
          dataType: 'json',
          headers: SpiderPanda.headers(
            'https://www.panda.tv/' + room.anchor.room),
        }),
    ]);

    room.data.num_follower = SpiderPanda.toNumber(follower.data.data.fans);

    //礼物价值
    {
      const match1 = res.data.match(/"bamboos":"(\d+)/);
      if (match1 && match1[1])
        room.data.num_gift = SpiderPanda.toNumber(match1[1]);
    }
  }

  /**
   * 生成列表页面的网址
   * @param page
   * @return string
   */
  static listPageUrl(page) {
    return `https://www.panda.tv/live_lists?status=2&pageno=${page}&pagenum=120&order=top`;
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
  }

}

module.exports = SpiderPanda;